package com.dycong.common.web;

import lombok.extern.log4j.Log4j;
import org.bson.Document;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Date;

/**
 * Created by dycong on 15-12-16.
 */
@Log4j
public class RequestLogFilter implements Filter {

    String rootKey;

    static String REQUEST_LOG_COLLECTION_NAME = "requestLog";


    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        rootKey = filterConfig.getServletContext().getInitParameter("webAppRootKey");
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest httpServletRequest = (HttpServletRequest) request;
        String uri = httpServletRequest.getRequestURI();
        saveLog("", uri);
    }

    @Override
    public void destroy() {

    }



    void saveLog(String json, String uri) {
        try {
            Document document = Document.parse(json)
                    .append("uri", uri)
                    .append("webAppRootKey", rootKey)
                    .append("createdTime", new Date());
//            MongoUtil.insertIfPossible(REQUEST_LOG_COLLECTION_NAME, document);
        } catch (Throwable e) {
            log.error("SAVE LOG ERROR", e);
        }
    }
}
